<?php
require_once(FWK_EXCEPTION."CustonException.class.php");
require_once(BIB_ACTIVE_RECORD);
require_once(FWK_MODEL."AbsModelDao.class.php");
require_once(FWK_MODEL."Upload.class.php");
require_once(FWK_UTIL."FormataDatas.class.php");
/**
 * Classe modelo para a estrutura da sessão do noticia que estiver logado
 *
 * @author André Coura <andreccls@gmail.com>
 * @since 1.0 - 02/02/2012
 */
class Banner extends AbsModelDao{

	public $_table = "banners";

	public $_id = "id_banner";

	/**
	 * @param int $limitIni
	 * @param int $numRegs
	 * @param boolean $count
	 */
	public function getListaGrid($limitIni=0, $numRegs=15, $count=false){
		$strQuery = "SELECT ";
		if($count){
			$strQuery.= " count(id_banner) ";
		}else{
			$strQuery .= "id_banner, nome_banner as 'Banner', if(status_banner='S','Ativo','Inativo') as 'Status'";
		}
		$strQuery .= " FROM
					 		".$this->_table. " ";
		
		$strQuery .= " ORDER BY nome_banner";
		
		if(!$count){
			$strQuery .= " LIMIT
								".$limitIni." ,".$numRegs;
		}
		$arrRet = ControlDB::getAll($strQuery,3);

		return $arrRet;
	}

	public function getListaBanners(){
		$strQuery = "SELECT
							id_banner, nome_banner, img_banner,
							status_banner, data_alteracao, id_usuario, link_banner
					 FROM 
					 		banners";
		$arrRet = ControlDB::getAll($strQuery,3);
		return $arrRet;
	}

	/**
	 * Busca dos banners para a Home
	 */
	public function getBanners(){
		$strQuery = "SELECT
							id_banner, nome_banner, img_banner,
							status_banner, data_alteracao, id_usuario, link_banner 
					 FROM 
					 		banners
					 WHERE
					 	    status_banner = 'S'
					 ORDER BY 
					 		data_alteracao DESC
					 LIMIT 
					 		0,6";
		$arrRet = ControlDB::getAll($strQuery,3);
		return $arrRet;
	}


	public function alterar($id=null,$post,$file){
		try{
			self::salvaPostAutoUtf8($post);
			$this->id_banner = $id;
				
			/**
			 * UPLOAD DO ARQUIVO
			 */
			parent::uploadArquivoFwk("img_banner", $this->id_banner, $file, PASTA_UPLOADS_ARQUIVOS."banners/");
				
			//campos default para todos os daos (historico de edição de arquivos)
			$this->data_alteracao = date("Y-m-d H:i:s");
			$this->id_usuario = parent::getUsuarioSessao()->getIdUsuario();
			self::replace();
			if(self::ErrorMsg()){
				print("<pre>");
				print_r($post);
				die("<br/><br /><h1>".self::ErrorMsg()."</h1>");
			}
		}catch(DaoException $e){
			throw new CustonException($e->getMensagem());
		}
	}
}
?>